<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            
            <el-table-column prop="title" label="公告标题" min-width="200">
              <template slot-scope="scope">
                <div class="notice-title">
                  <el-tag v-if="scope.row.is_top === 1" type="danger" size="mini" style="margin-right: 5px;">置顶</el-tag>
                  <el-tag v-if="scope.row.is_recommend === 1" type="warning" size="mini" style="margin-right: 5px;">推荐</el-tag>
                  {{ scope.row.title }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="position" label="公告位置" width="120">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.position === 'interview'" type="primary">首页</el-tag>
                <el-tag v-else type="info">{{ scope.row.position }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="content" label="公告内容" min-width="300">
              <template slot-scope="scope">
                <div class="notice-content" v-html="scope.row.content"></div>
              </template>
            </el-table-column>
            <el-table-column prop="status" label="状态" width="100">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status === 1" type="success">启用</el-tag>
                <el-tag v-else type="info">禁用</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="sort" label="排序" width="80"/>
            <el-table-column prop="view_count" label="浏览次数" width="100"/>
            <el-table-column prop="start_time" label="开始时间" width="160">
              <template slot-scope="scope">
                {{ formatDateTime(scope.row.start_time) || '无限制' }}
              </template>
            </el-table-column>
            <el-table-column prop="end_time" label="结束时间" width="160">
              <template slot-scope="scope">
                {{ formatDateTime(scope.row.end_time) || '无限制' }}
              </template>
            </el-table-column>
            <el-table-column prop="create_time" label="创建时间" width="160">
              <template slot-scope="scope">
                {{ formatDateTime(scope.row.create_time) }}
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="200">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" @click="onView(scope.row)">查看</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
    <View ref="viewFormRef" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";
import View from "./view";

export default {
  name: "NoticeIndex",
  components: {
    Add,
    View
  },
  mixins: [crud],
  data() {
    return {
      request: http.notice,
      searchList: [
        {
          label: '公告标题',
          prop: 'title',
          name: 'input',
          placeholder: '请输入公告标题'
        },
        {
          label: '公告位置',
          prop: 'position',
          name: 'select',
          placeholder: '请选择公告位置',
          options: [
            { value: 'interview', label: '面试' },
            { value: 'written', label: '笔试' }
          ]
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          placeholder: '请选择状态',
          options: [
            { value: 1, label: '启用' },
            { value: 0, label: '禁用' }
          ]
        },
        {
          label: '是否置顶',
          prop: 'is_top',
          name: 'select',
          placeholder: '请选择是否置顶',
          options: [
            { value: 1, label: '是' },
            { value: 0, label: '否' }
          ]
        },
        {
          label: '是否推荐',
          prop: 'is_recommend',
          name: 'select',
          placeholder: '请选择是否推荐',
          options: [
            { value: 1, label: '是' },
            { value: 0, label: '否' }
          ]
        }
      ],
      searchParams: {
        title: '',
        position: '',
        status: '',
        is_top: '',
        is_recommend: ''
      },
      defaultParams: {
      },
      tableData: []
    }
  },
  methods: {
    formatDateTime(dateTime) {
      if (!dateTime) return '';
      return new Date(dateTime).toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      });
    },
    onView(row) {
      this.$refs.viewFormRef.open(row);
    }
  }
}
</script>

<style scoped lang="scss">
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
.notice-title {
  display: flex;
  align-items: center;
}
.notice-content {
  max-height: 60px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
</style>
